package com.diyunapp.xilinapp.me;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.fragments.CollectGoodsFragment;
import com.diyunapp.xilinapp.fragments.CollectShopFragment;
import com.diyunapp.xilinapp.selfdefine.FragmentAdapter;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCollectActivity extends StatusBarFragmentActivity implements ViewPager.OnPageChangeListener {
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.edit_zuji)
    TextView editZuji;
    @Bind(R.id.clect_goods_tv)
    TextView clectGoodsTv;
    @Bind(R.id.clect_shop_tv)
    TextView clectShopTv;
    @Bind(R.id.invest_view_pager)
    ViewPager investViewPager;
    @Bind(R.id.goods_line)
    TextView goodsLine;
    @Bind(R.id.shop_line)
    TextView shopLine;
    private View headView;

    private List<Fragment> myCollectList;
    private FragmentAdapter myCollectPagerAp;

    private Fragment collectGoodsFrag;
    private Fragment collectShopFrag;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private MyCollectPagerAdapter myCollectPagerAdapter;
    private List<Fragment> fragments;
    private String[] tabs = {"商品", "店铺"};


    private OnButtonClickedListener buttonClickedListener;
    private OnButtonClickedListener1 buttonClickedListener1;
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg != null) {
                switch (msg.what) {
                    case 100:


                        break;

                    default:
                        break;
                }
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        ButterKnife.bind(this);
        headView = findViewById(R.id.mycollect_menubar_inc);
        super.MenuWidget(headView);
        Log.i("xiaogalade", "buttonClickedListener" + buttonClickedListener);
        initView();
        clickRadioButton(0);
    }

    @OnClick({R.id.edit_zuji, R.id.go_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
                break;
            case R.id.edit_zuji:

                if (editZuji.getText().toString() == "编辑" || editZuji.getText().toString().equals("编辑")) {
//                    Log.i("xiaogalade编辑","buttonClickedListener"+buttonClickedListener);

                    if (buttonClickedListener != null) {
                        buttonClickedListener.onclicked("1");
                    }
                    if (buttonClickedListener1 != null) {
                        buttonClickedListener1.onclicked("1");
                    }
                    editZuji.setText("完成");
                } else if (editZuji.getText().toString() == "完成" || editZuji.getText().toString().equals("完成")) {
//                    Log.i("xiaogalade完成","buttonClickedListener"+buttonClickedListener);

                    if (buttonClickedListener != null) {
                        buttonClickedListener.onclicked("2");
                    }
                    if (buttonClickedListener1 != null) {
                        buttonClickedListener1.onclicked("2");
                    }
                    editZuji.setText("编辑");
                }
                break;
            default:
                break;
        }
    }

    public void initView() {
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        myCollectList = new ArrayList<>();
        collectGoodsFrag = new CollectGoodsFragment();
        collectShopFrag = new CollectShopFragment();
        myCollectList.add(collectGoodsFrag);
        myCollectList.add(collectShopFrag);
        myCollectPagerAp = new FragmentAdapter(this.getSupportFragmentManager(), myCollectList);
        investViewPager.setAdapter(myCollectPagerAp);
        investViewPager.setOnPageChangeListener(this);
        myCollectPagerAp.notifyDataSetChanged();
//        myCollectPagerAdapter = new MyCollectPagerAdapter(getSupportFragmentManager());
//        viewPager = (ViewPager) findViewById(R.id.invest_view_pager);
//        viewPager.setAdapter(myCollectPagerAdapter);
//        tabLayout = (TabLayout) findViewById(R.id.invest_tab);
//        tabLayout.setTabMode(TabLayout.MODE_FIXED);
//        tabLayout.setTabsFromPagerAdapter(myCollectPagerAdapter);
//        tabLayout.setupWithViewPager(viewPager);

    }

    private void clickRadioButton(int state) {
        switch (state) {
            case 0:
                goodsLine.setVisibility(View.VISIBLE);
                shopLine.setVisibility(View.GONE);
                break;
            case 1:
                goodsLine.setVisibility(View.GONE);
                shopLine.setVisibility(View.VISIBLE);
                break;

            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.i("zhangxiaoxidebug", "evaluation position=" + position);
        clickRadioButton(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class MyCollectPagerAdapter extends FragmentStatePagerAdapter {


        public MyCollectPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments == null ? null : fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments == null ? 0 : fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
    }

    public interface OnButtonClickedListener {
        /**
         * 里面传个值
         *
         * @param s
         */
        public void onclicked(String s);
    }

    public interface OnButtonClickedListener1 {
        /**
         * 里面传个值
         *
         * @param s
         */
        public void onclicked(String s);
    }

    /**
     * @param buttonClickedListener 写一个对外公开的方法
     */
    public void setButtonClickedListener(OnButtonClickedListener buttonClickedListener) {
        this.buttonClickedListener = buttonClickedListener;
    }

    public void setButtonClickedListener1(OnButtonClickedListener1 buttonClickedListener1) {
        this.buttonClickedListener1 = buttonClickedListener1;
    }
}
